<?php
// 设置页面编码格式
header('content-type:text/html;charset=utf-8');
// 地址，用户名，密码。数据库名，端口号
$sqlObj = new mysqli('127.0.0.1', 'root', 'root', 'paipaiweb', '3306');
// $sqlObj = new mysqli('127.0.0.1','root','root','ch191208cws','3306');

if ($sqlObj->connect_errno != 0) {
    echo '连接失败';
} else {
    switch ($_POST['type']) {
        // 1用户登录
        case 'userLogin':
            // echo '连接成功';
            $account = $_POST['account'];
            $psw = $_POST['psw'];

            // 声明sql语句     '.$phone.'     '.$_psw.'
            $sql = 'select * from pai_user where pai_user_account = "' . $account . '" and pai_user_psw = "' . $psw . '" AND pai_user_status = "正常"';
            // 执行sql语句 
            $result = $sqlObj->query($sql);
            // 解析单条数据数据
            $info = $result->fetch_assoc();
            //  print_r ($info);
            if ($info) {
                // echo '登入成功';
                echo json_encode(array('code' => 10000, 'msg' => '登入成功', 'data' => $account));
            } else {
                // echo '登入失败，账号或密码错误';
                echo json_encode(array('code' => 10001, 'msg' => '登入失败，账号或密码错误'));
            };
            break;

        // 1用户注册
        case 'userRegister':
            // echo '连接成功';
            $account = $_POST['account'];
            $psw = $_POST['psw'];

            // 声明sql语句     '.$account.'     '.$_psw.'
            $sql1 = 'select * from pai_user where pai_user_account = "' . $account . '"';
            $sql2 = 'INSERT INTO pai_user(pai_user_account,pai_user_psw) VALUES("' . $account . '","' . $psw . '")';
            // 执行sql语句
            $result = $sqlObj->query($sql1);
            // 解析单条数据数据
            $info = $result->fetch_assoc();


            // 判断 info bool值
            if ($info) {
                echo json_encode(array('code' => 10004, 'msg' => '账号已存在'));
                // echo '账号已存在';
            } else {
                if ($sqlObj->query($sql2)) {
                    // echo '注册成功';
                    echo json_encode(array('code' => 10002, 'msg' => '注册成功'));
                } else {
                    // echo '注册失败';
                    echo json_encode(array('code' => 10003, 'msg' => '注册失败'));
                };
            };
            break;

        // 用户修改密码
        case 'updatePsw':
            $psw = $_POST['psw'];
            $account = $_POST['account'];
            
            $sql = 'UPDATE pai_user SET pai_user_psw = "'.$psw.'" WHERE pai_user_account = "'.$account.'"';

            if ($sqlObj -> query($sql)) {
                echo json_encode(array('code' => 00001,'msg' => '修改成功'));
            }else{
                echo json_encode(array('code' => 00000,'msg' => '修改失败'));

            }
            
        break;
        
        // 1管理员登录
        case 'adminLogin':
            // echo '连接成功';
            $admin = $_POST['admin'];
            $pass = $_POST['pass'];
            // 声明sql语句     '.$count.'     '.$pwd.'
            $sql = 'select * from pai_admin where pai_admin_acc = "' . $admin . '" and pai_admin_pass = "' . $pass . '"';
            // 执行sql语句
            $result = $sqlObj->query($sql);
            // 解析单条数据数据
            $info = $result->fetch_assoc();
            // print_r ($info);
            if ($info) {
                // echo '登入成功';
                echo json_encode(array('code' => 20000, 'msg' => '登入成功', 'data' => $admin));
            } else {
                // echo '登入失败，账号或密码错误';
                echo json_encode(array('code' => 20001, 'msg' => '登入失败，账号或密码错误'));
            };
            break;

        // 1搜索图书请求
        case 'getSerach':

            $searchTitle = $_POST['searchTitle'];

            $sql = 'SELECT * FROM pai_goods where pai_goods_name like "%' . $searchTitle . '%" OR pai_goods_info like "%' . $searchTitle . '%" OR pai_goods_kinds like "%' . $searchTitle . '%"';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 50040, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 50041, 'msg' => '找到相关商品'));
            }
            break;




        // 1上传商品
        case 'uploadBook':
            // 获取了文件的类型
            $type = $_FILES['img']['type'];
            // 获取了文件的大小
            $size = $_FILES['img']['size'];
            // 获取了文件的临时存储路径
            $tmpName = $_FILES['img']['tmp_name'];
            if ($type != 'image/jpeg') {
                echo json_encode(array('code' => 10031, 'msg' => '文件类型错误'));
            } else if ($size >= 8388608) {
                echo json_encode(array('code' => 10032, 'msg' => '文件大小过大'));
            } else {
                $imgPath = 'images/hotImg/';

                // 手动创建路径 
                if (!file_exists($imgPath)) {
                    mkdir($imgPath, '0754');
                }
                // 获取当前的时间戳
                $picUrl = $imgPath . time() . '.jpg';
                $result = move_uploaded_file($tmpName, $picUrl);
                $imgP = '../../src/' . $picUrl;
                if ($result) {
                    $sql = "INSERT INTO pai_goods(pai_goods_num,pai_goods_name,pai_goods_info,pai_goods_price,pai_goods_img,pai_goods_kinds,pai_goods_master) VALUES('" . $_POST['goodsNum'] . "', '" . $_POST['name'] . "','" . $_POST['info'] . "','" . $_POST['price'] . "','" . $imgP . "','" . $_POST['kinds'] . "','" . $_POST['master'] . "');";

                    $result = $sqlObj->query($sql);

                    if ($result) {
                        echo json_encode(array('code' => 10030, 'msg' => '上传成功'));
                    } else {
                        echo json_encode(array('code' => 10034, 'msg' => '上传失败'));
                    };
                } else {
                    echo json_encode(array('code' => 10033, 'msg' => '上传失败'));
                };
            };
            break;

        // 1获取售卖物件 
        case 'sellMange':

            $master = $_POST['master'];

            $sql = 'SELECT * FROM pai_goods where pai_goods_master = "' . $master . '" ORDER BY pai_goods_id DESC;';
            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 10040, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 10041, 'msg' => '获取失败'));
            }
            break;


        // 1获取售卖订单 
        case 'sellOrder':

            $master = $_POST['master'];

            $sql = 'SELECT * FROM pai_sell where pai_sell_master = "' . $master . '" ORDER BY pai_sell_id DESC;';
            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 10040, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 10041, 'msg' => '获取失败'));
            }
            break;
        
        // 1删除订单
        case 'delOrder':
            $order_id = $_POST['order_id'];
            $account = $_POST['account'];

            $sql = 'DELETE FROM pai_order WHERE pai_order_id = "' . $order_id . '" AND pai_order_account  = "' . $account . '";';

            $result = $sqlObj->query($sql);


            if ($result) {
                $sql = 'SELECT * FROM pai_order where pai_order_account ="' . $account . '" ORDER BY pai_order_id DESC;';

                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 10050, 'msg' => '删除成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 10051, 'msg' => '删除失败'));
            }
            break;

        // 1用户下架商品
        case 'shelfGoods':
            $goodsID = $_POST['goodsID'];
            $account = $_POST['account'];

            $sql = 'DELETE FROM pai_goods WHERE pai_goods_id = "' . $goodsID . '" AND pai_goods_master  = "' . $account . '";';
            $result = $sqlObj->query($sql);


            if ($result) {
                $sql = 'SELECT * FROM pai_goods where pai_goods_master = "' . $account . '";';
                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 10090, 'msg' => '下架成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 10091, 'msg' => '下架失败'));
            }
            break;

        // 1用户管理
        case 'userManage':

            $sql = 'SELECT * FROM pai_user';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 10060, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 10061, 'msg' => '获取失败'));
            }
            break;

        // 1用户封禁
        case 'modifyState':
            $account = $_POST['account'];


            $status = $_POST['status'];

            if ($status == '封禁') {
                $sql = 'update pai_user set pai_user_status= "' . $status . '" where pai_user_account ="' . $account . '";';

                $sql2 = 'delete from pai_goods where pai_goods_master = "' . $account . '"';

                $result = $sqlObj->query($sql);
                $result2 = $sqlObj->query($sql2);

                if ($result && $result2) {
                    $sql = 'SELECT * FROM pai_user';
                    // 执行命令
                    $result = $sqlObj->query($sql);
                    // 解析命令
                    $info = $result->fetch_all(MYSQLI_ASSOC);
                    echo json_encode(array('code' => 60031, 'msg' => '状态修改成功', 'data' => $info));
                } else {
                    echo json_encode(array('code' => 60030, 'msg' => '状态修改失败'));
                }
            } else {
                $sql = 'update pai_user set pai_user_status= "' . $status . '" where pai_user_account ="' . $account . '";';

                $result = $sqlObj->query($sql);

                if ($result) {
                    $sql = 'SELECT * FROM pai_user';
                    // 执行命令
                    $result = $sqlObj->query($sql);
                    // 解析命令
                    $info = $result->fetch_all(MYSQLI_ASSOC);
                    echo json_encode(array('code' => 60031, 'msg' => '状态修改成功', 'data' => $info));
                } else {
                    echo json_encode(array('code' => 60030, 'msg' => '状态修改失败'));
                }
            }

            break;


        // 1热卖图书请求
        case 'getHotGooods':

            $type = $_POST['goodsType'];

            $sql = 'SELECT * FROM pai_goods where pai_goods_kinds = "' . $type . '" ORDER BY pai_goods_id DESC ;';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 20010, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 20011, 'msg' => '获取失败'));
            }
            break;

        // 1商品详情页
        case 'goodsPage':

            $goodsID = $_POST['goodsID'];

            $sql = 'SELECT * FROM pai_goods where pai_goods_id = "' . $goodsID . '";';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 20020, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 20021, 'msg' => '获取失败'));
            }
            break;

        // 1评论获取
        case 'commentPage':
            $goodsID = $_POST['goodsID'];

            $sql = 'SELECT * FROM pai_comment where pai_goods_id = "' . $goodsID . '";';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 99999, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 99999, 'msg' => '获取失败'));
            }
            break;

        // 1下单
        case 'createOrder':
            $account = $_POST['account'];
            $goodsID = $_POST['goodsID'];
            $orderDate = $_POST['orderDate'];
            $goodsName = $_POST['goodsName'];
            $goodsPrice = $_POST['goodsPrice'];
            $sellMaster = $_POST['sellMaster'];
            $goodsNum = $_POST['goodsNum'];


            $sql = 'INSERT INTO pai_order(pai_order_goods_num,pai_order_account,pai_order_goods_id,pai_order_data,pai_order_goods_name,pai_order_goods_price,pai_sell_master) VALUES("' . $goodsNum . '","' . $account . '","' . $goodsID . '","' . $orderDate . '","' . $goodsName . '","' . $goodsPrice . '","' . $sellMaster . '");';

            // // 执行命令
            // $result = $sqlObj->query($sql);
            // 解析命令
            // $info = $result -> fetch_all(MYSQLI_ASSOC);

            if ($sqlObj->query($sql)) {
                echo json_encode(array('code' => 30001, 'msg' => '已成功加入购物车'));
            } else {
                echo json_encode(array('code' => 30002, 'msg' => '订单失败'));
            }
            break;



        // 获取用户个人信息
        case 'getUserinfo':
            $account = $_POST['account'];

            $sql = 'SELECT * FROM pai_user where pai_user_account = "' . $account . '";';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_assoc();

            if ($sqlObj->query($sql)) {
                echo json_encode(array('code' => 30010, 'msg' => '获取个人信息成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 30011, 'msg' => '获取个人信息失败'));
            }
            break;

        // 1充值
        case 'addMoney':
            $account = $_POST['account'];
            $money = $_POST['money'];

            $sql = 'update pai_user set pai_user_money=pai_user.pai_user_money +"' . $money . '"  where pai_user_account ="' . $account . '";';

            // 执行命令
            // $result = $sqlObj->query($sql);
            // 解析命令
            // $info = $result -> fetch_assoc();

            if ($sqlObj->query($sql)) {
                $sql = 'SELECT pai_user_money FROM pai_user where pai_user_account ="' . $account . '";';
                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_assoc();
                echo json_encode(array('code' => 30020, 'msg' => '充值成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 30021, 'msg' => '获取个人信息失败'));
            }
            break;


        // 1所有订单
        case 'Trade':
            // 账号id
            $account = $_POST['account'];

            // 查找商品信息
            $sql = 'SELECT * FROM pai_order where pai_order_account ="' . $account . '"ORDER BY pai_order_id DESC;';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);
            if ($info) {
                echo json_encode(array('code' => 40000, 'msg' => '获取所有订单信息成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 40000, 'msg' => '获取所有订单信息成功', 'data' => $info));
            }
            break;

        // 1付款
        case 'payment':
            // 订单号
            $order_id = $_POST['order_id'];
            // 商品名称
            $goods_name = $_POST['goods_name'];
            // 数量
            $order_num = $_POST['order_num'];
            // 商品价格
            $order_price = $_POST['order_price'];
            // 用户买家账号
            $account = $_POST['account'];
            // 卖家
            $master = $_POST['master'];
            // 订单时间
            $order_data = $_POST['order_data'];
            // 书籍ID 
            $goods_id = $_POST['goods_id'];
            

            $sql4 = 'INSERT INTO pai_sell(pai_sell_order_id,pai_sell_name,pai_sell_num,pai_sell_price,pai_sell_account,pai_sell_master,pai_sell_data) VALUES("' . $order_id . '","' . $goods_name . '","' . $order_num . '","' . $order_price . '","' . $account . '","' . $master . '","' . $order_data . '");';

            // 从账号中扣钱
            $sql1 = 'update pai_user set pai_user_money=pai_user.pai_user_money -('.$order_price.' * '.$order_num.')  where pai_user_account ="' . $account . '";';
            // 修改对应账号的对应的书籍ID的订单状态
            $sql2 = 'update pai_order set pai_order_status = "待收货"  where pai_order_account ="' . $account . '" AND pai_order_id = "' . $order_id . '" ;';
            // 商品数量减1
            $sql3 = 'update pai_goods set pai_goods_num=pai_goods.pai_goods_num - "'.$order_num.'"  where pai_goods_id ="' . $goods_id . '";';
            // 商品状态改变
            // $sql4 = 'UPDATE pai_goods SET pai_goods_status = "待收货" WHERE pai_goods_id = "' . $goods_id . '";';


            if ($sqlObj->query($sql1) && $sqlObj->query($sql2) && $sqlObj->query($sql3) && $sqlObj->query($sql4)) {
                // 查询账号所有信息 目的是为了拿到账号里的钱
                $sql1 = 'SELECT * FROM pai_user where pai_user_account ="' . $account . '";';
                // 再次查询该账号的所有订单
                // $sql2 = 'SELECT pai_goods_id,pai_goods_name,pai_goods_price,pai_order_data,pai_order_status,pai_order_evaluate FROM pai_goods INNER JOIN pai_order where pai_goods.pai_goods_id = pai_order.pai_order_goods_id AND pai_order_account = "'.$account.'";';
                $sql2 = 'SELECT * FROM pai_order where pai_order_account ="' . $account . '"ORDER BY pai_order_id DESC;';

                // 执行命令
                $result1 = $sqlObj->query($sql1);
                // 解析命令
                $info1 = $result1->fetch_assoc();

                // 执行命令
                $result2 = $sqlObj->query($sql2);
                // 解析命令
                $info2 = $result2->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 30030, 'msg' => '付款成功', 'userData' => $info1, 'orderData' => $info2));
            } else {
                echo json_encode(array('code' => 30031, 'msg' => '付款失败'));
            }
            break;


        // 1 多个商品付款
        case 'totalPay':

            $orderArr = $_POST['totalOrder'];
            for ($x = 0; $x < count($orderArr); $x++) {
                $sql1 = 'update pai_user set pai_user_money=pai_user.pai_user_money -(' . $orderArr[$x]['pai_order_goods_price'] . ' * ' . $orderArr[$x]['pai_order_goods_num'] . ')  where pai_user_account ="' . $orderArr[$x]['pai_order_account'] . '";';
                $sql2 = 'update pai_goods set pai_goods_num=pai_goods.pai_goods_num - "'.$orderArr[$x]['pai_order_goods_num'].'"  where pai_goods_id ="' . $orderArr[$x]['pai_order_goods_id'] . '";';
                $sql3 = 'UPDATE pai_order SET pai_order_status = "待收货" WHERE pai_order_id = "' . $orderArr[$x]['pai_order_id'] . '";';
                $sql4 = 'INSERT INTO pai_sell(pai_sell_order_id,pai_sell_name,pai_sell_num,pai_sell_price,pai_sell_account,pai_sell_master,pai_sell_data) VALUES("' . $orderArr[$x]['pai_order_id'] . '","'  . $orderArr[$x]['pai_order_goods_name'] . '","' . $orderArr[$x]['pai_order_goods_num'] . '","' . $orderArr[$x]['pai_order_goods_price'] . '","' . $orderArr[$x]['pai_order_account'] . '","' . $orderArr[$x]['pai_sell_master'] . '","' . $orderArr[$x]['pai_order_data'] . '");';

            
                $result1 = $sqlObj->query($sql1);
                $result2 = $sqlObj->query($sql2);
                $result3 = $sqlObj->query($sql3);
                $result4 = $sqlObj->query($sql4);
            }
            // 查询账号所有信息 目的是为了拿到账号里的钱
            $sql1 = 'SELECT * FROM pai_user where pai_user_account ="' . $orderArr[0]['pai_order_account'] . '";';
            // 再次查询该账号的所有订单
            $sql2 = 'SELECT * FROM pai_order where pai_order_account ="' . $orderArr[0]['pai_order_account'] . '"ORDER BY pai_order_id DESC;';

            // 执行命令
            $result1 = $sqlObj->query($sql1);
            // 解析命令
            $info1 = $result1->fetch_assoc();

            // 执行命令
            $result2 = $sqlObj->query($sql2);
            // 解析命令
            $info2 = $result2->fetch_all(MYSQLI_ASSOC);
            echo json_encode(array('code' => 30030, 'msg' => '付款成功', 'userData' => $info1, 'orderData' => $info2));



            break;

        // 1收货
        case 'checkReceive':
            // 用户账号
            $account = $_POST['account'];
            // 书籍ID
            $goods_id = $_POST['goods_id'];
            // 订单号
            $order_id = $_POST['order_id'];

            // 修改对应账号的对应的订单状态
            $sql = 'update pai_order set pai_order_status = "待评价"  where pai_order_account ="' . $account . '" AND pai_order_id = "' . $order_id . '";';

            $sql2 = 'UPDATE pai_sell SET pai_sell_status = "待评价" WHERE pai_sell_order_id = "' . $order_id . '";';

            if ($sqlObj->query($sql)  && $sqlObj->query($sql2)) {
                // 再次查询该账号的所有订单
                // $sql = 'SELECT * FROM bookweb_order where bookWeb_order_phone ="'.$phone.'";';
                // $sql = 'SELECT pai_goods_id,pai_goods_name,pai_goods_price,pai_order_data,pai_order_status,pai_order_evaluate FROM pai_goods INNER JOIN pai_order where pai_goods.pai_goods_id = pai_order.pai_order_goods_id AND pai_order_account = "'.$account.'";';
                $sql = 'SELECT * FROM pai_order where pai_order_account ="' . $account . '"ORDER BY pai_order_id DESC;';

                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 30040, 'msg' => '收货成功', 'orderData' => $info));
            } else {
                echo json_encode(array('code' => 30041, 'msg' => '收货失败'));
            }
            break;



        // 1评论
        case 'evaluate':
            // 用户账号
            $account = $_POST['account'];
            // 书籍ID
            $goods_id = $_POST['goods_id'];
            // 订单号
            $order_id = $_POST['order_id'];
            // 用户评价星级
            $eval_value = $_POST['eval_value'];
            $content = $_POST['content'];
            $date = $_POST['date'];

            // 修改对应账号的对应的书籍ID的订单状态
            $sql1 = 'update pai_order set pai_order_evaluate = "' . $eval_value . '"  where pai_order_account ="' . $account . '" AND pai_order_id = "' . $order_id . '" ;';
            $sql2 = 'update pai_order set pai_order_status = "已评价"  where pai_order_account ="' . $account . '" AND pai_order_id = "' . $order_id . '" ;';
            $sql3 = 'UPDATE pai_sell SET pai_sell_status = "已评价" WHERE pai_sell_order_id = "' . $order_id . '";';

            $sql4 = 'INSERT INTO pai_comment(pai_comment_account,pai_comment_content,pai_comment_evaluate,pai_goods_id,pai_comment_date) VALUES("' . $account . '","' . $content . '","' . $eval_value . '","' . $goods_id . '","' . $date . '");';

            if ($sqlObj->query($sql1) && $sqlObj->query($sql2) && $sqlObj->query($sql3) && $sqlObj->query($sql4)) {
                // 再次查询该账号的所有订单
                // $sql = 'SELECT * FROM pai_order where pai_order_account ="'.$account.'";';
                // $sql = 'SELECT pai_goods_id,pai_goods_name,pai_goods_price,pai_order_data,pai_order_status,pai_order_evaluate FROM pai_goods INNER JOIN pai_order where pai_goods.pai_goods_id = pai_order.pai_order_goods_id AND pai_order_account = "'.$account.'";';
                $sql = 'SELECT * FROM pai_order where pai_order_account ="' . $account . '"ORDER BY pai_order_id DESC;';

                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 30050, 'msg' => '评价成功', 'orderData' => $info));
            } else {
                echo json_encode(array('code' => 30051, 'msg' => '收货失败'));
            }
            break;

        // 1退款
        case 'refund':
            // 买家账号
            $account = $_POST['account'];
            // 商品价格
            $price = $_POST['price'];
            // 订单号
            $order_id = $_POST['order_id'];
            // 商品名称
            $name = $_POST['name'];
            // 卖家账号
            $master = $_POST['master'];

            // 修改对应账号的对应的书籍ID的订单状态
            $sql1 = 'update pai_order set pai_order_status = "退款中"  where pai_order_account ="' . $account . '" AND pai_order_id = "' . $order_id . '" ;';
            // 退单插入
            $sql2 = 'INSERT INTO pai_refund(pai_refund_account,pai_refund_master,pai_refund_goods_price,pai_refund_order_id,pai_refund_goods_name) VALUES("' . $account . '","' . $master . '","' . $price . '","' . $order_id . '","' . $name . '");';
            // 卖家的单子
            $sql3 = 'update pai_sell set pai_sell_status = "退款中"  where  pai_sell_order_id = "' . $order_id . '" ;';

            if ($sqlObj->query($sql1) && $sqlObj->query($sql2) && $sqlObj->query($sql3)) {
                // 再次查询该账号的所有订单
                // $sql = 'SELECT * FROM pai_order where pai_order_account ="'.$account.'";';
                // $sql = 'SELECT pai_goods_id,pai_goods_name,pai_goods_price,pai_order_data,pai_order_status,pai_order_evaluate FROM pai_goods INNER JOIN pai_order where pai_goods.pai_goods_id = pai_order.pai_order_goods_id AND pai_order_account = "'.$account.'";';
                $sql = 'SELECT * FROM pai_order where pai_order_account ="' . $account . '"ORDER BY pai_order_id DESC;';

                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 50000, 'msg' => '退款成功', 'orderData' => $info));
            } else {
                echo json_encode(array('code' => 50001, 'msg' => '退款失败'));
            }
            break;

        // 1获取退款单
        case 'getRefund':

            $master = $_POST['master'];

            $sql = 'SELECT * FROM pai_refund where pai_refund_master = "' . $master . '" ORDER BY pai_refund_id DESC;';
            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 50010, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 50011, 'msg' => '获取失败'));
            }
            break;

        // 确认退款
        case 'checkRefund':
            // 买家账号
            $account = $_POST['account'];
            // 商品价格
            $price = $_POST['price'];
            // 订单号
            $order_id = $_POST['order_id'];
            // 卖家账号
            $master = $_POST['master'];

            // 从卖家账号中扣钱
            $sql1 = 'update pai_user set pai_user_money=pai_user.pai_user_money -"' . $price . '"  where pai_user_account ="' . $master . '";';
            // 从买家账号中加钱
            $sql2 = 'update pai_user set pai_user_money=pai_user.pai_user_money +"' . $price . '"  where pai_user_account ="' . $account . '";';
            // 修改买家的订单状态
            $sql3 = 'update pai_order set pai_order_status = "退款完成"  where pai_order_account ="' . $account . '" AND pai_order_id = "' . $order_id . '" ;';
            // 修改卖家退款单子状态
            $sql4 = 'UPDATE pai_refund SET pai_refund_status = "退款完成" WHERE pai_refund_order_id = "' . $order_id . '";';
            $sql5 = 'UPDATE pai_sell SET pai_sell_status = "退款完成" WHERE pai_sell_order_id = "' . $order_id . '";';


            if ($sqlObj->query($sql1) && $sqlObj->query($sql2) && $sqlObj->query($sql3) && $sqlObj->query($sql4) && $sqlObj->query($sql5)) {
                // 查询账号所有信息 目的是为了拿到账号里的钱
                $sql1 = 'SELECT * FROM pai_user where pai_user_account ="' . $master . '";';
                // 再次查询该账号的所有订单
                $sql2 = 'SELECT * FROM pai_refund where pai_refund_master ="' . $master . '"ORDER BY pai_refund_id DESC;';

                // 执行命令
                $result1 = $sqlObj->query($sql1);
                // 解析命令
                $info1 = $result1->fetch_assoc();

                // 执行命令
                $result2 = $sqlObj->query($sql2);
                // 解析命令
                $info2 = $result2->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 50020, 'msg' => '退款完成', 'userData' => $info1, 'data' => $info2));
            } else {
                echo json_encode(array('code' => 50021, 'msg' => '退款失败'));
            }
            break;

        // 卖家删除退款完成订单
        case 'delRefund':
            $refund_id = $_POST['refund_id'];
            $master = $_POST['master'];

            $sql = 'DELETE FROM pai_refund WHERE pai_refund_id = "' . $refund_id . '" AND pai_refund_master  = "' . $master . '";';
            $result = $sqlObj->query($sql);


            if ($result) {
                $sql = 'SELECT * FROM pai_refund where pai_refund_master = "' . $master . '" ORDER BY pai_refund_id DESC;';
                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 50030, 'msg' => '删除成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 50031, 'msg' => '删除失败'));
            }
            break;
        
        // 1.卖家删除销售订单
        case'delShopOrder':
            $order_id = $_POST['order_id'];
            $master = $_POST['master'];

            $sql = 'DELETE FROM pai_sell WHERE pai_sell_order_id = "' . $order_id . '" AND pai_sell_master  = "' . $master . '";';
            $result = $sqlObj->query($sql);


            if ($result) {
                $sql = 'SELECT * FROM pai_sell where pai_sell_master = "' . $master . '" ORDER BY pai_sell_id DESC;';
                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 50030, 'msg' => '删除成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 50031, 'msg' => '删除失败'));
            }
            break;
            
        // 1管理员商品请求
        case 'getgoods':

            $sql = 'SELECT * FROM pai_goods ';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 60001, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 60000, 'msg' => '获取失败'));
            }
            break;

        // 1所有商品类别数量查询
        case 'getGoodsType':

            $sql = 'select pai_goods_kinds,sum(pai_goods_num) as pai_goods_num from pai_goods group by pai_goods_kinds;';

            // 执行命令
            $result = $sqlObj->query($sql);
            // 解析命令
            $info = $result->fetch_all(MYSQLI_ASSOC);

            if ($info) {
                echo json_encode(array('code' => 60011, 'msg' => '获取成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 60010, 'msg' => '获取失败'));
            }
            break;

        
        // 1管理员下架商品
        case 'shelfAdmin':
            $goodsID = $_POST['goodsID'];

            $sql = 'DELETE FROM pai_goods WHERE pai_goods_id = "' . $goodsID . '";';

            $result = $sqlObj->query($sql);

            if ($result) {
                $sql = 'SELECT * FROM pai_goods ';
                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result->fetch_all(MYSQLI_ASSOC);
                echo json_encode(array('code' => 60021, 'msg' => '下架成功', 'data' => $info));
            } else {
                echo json_encode(array('code' => 60020, 'msg' => '下架失败'));
            }
            break;



        default:
            # code...
            break;
    }
}
